<?php
    /**
     * Application model for CakePHP.
     *
     * Licensed under The APACHE 2.0 License
     * For full copyright and license information, please see the LICENSE.txt
     * Redistributions of files must retain the above copyright notice.
     *
     * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
     * @link          http://cakephp.org CakePHP(tm) Project
     * @package       app.Model
     * @since         CakePHP(tm) v 0.2.9
     * @license       http://www.opensource.org/licenses/mit-license.php MIT License
     */

    App::uses( 'Model', 'Model' );

    /**
     * Application model for Cake.
     *
     * Add your application-wide methods in the class below, your models
     * will inherit them.
     *
     * @package       app.Model
     */
    class User extends AppModel {

        public $actsAs = array( 'Containable' );

        public $hasMany = array(
            'Registration'     => array(
                'className'  => 'Registration',
                'foreignKey' => 'id_user'
            ),
            'RegistrationProf' => array(
                'className'  => 'Registration',
                'foreignKey' => 'id_prof'
            ),
            'Theme'            => array(
                'className'  => 'Theme',
                'foreignKey' => 'id_user'
            ),
            "UnapprovedThemes" => array(
                'className'  => 'Theme',
                'foreignKey' => 'id_user',
                "conditions" => array(
                    "id_stud" => 0
                )
            ),
            'ThemeStud'        => array(
                'className'  => 'Theme',
                'foreignKey' => 'id_stud'
            ),
        );

        var $validate = array(
            "nume"     => array(
                "numeNotEmpty" => array(
                    "rule"    => "notEmpty",
                    "message" => "Numele nu poate fi gol"
                )
            ),
            "prenume"  => array(
                "prenumeNotEmpty" => array(
                    "rule"    => "notEmpty",
                    "message" => "Prenumele nu poate fi gol"
                )
            ),
            "email"    => array(
                "numeNotEmpty" => array(
                    "rule"    => "notEmpty",
                    "message" => "Emailul nu poate fi gol"
                ),
                "emailUnic"    => array(
                    "rule"    => "isUnique",
                    "message" => "Emailul este deja folosit"
                ),
                "tipEmail"     => array(
                    "rule"    => "email",
                    "message" => "Emailul nu este valid"
                ),
            ),
            "username" => array(
                "usernameNotEmpty" => array(
                    "rule"    => "notEmpty",
                    "message" => "Username-ul nu poate fi gol"
                ),
                "usernameUnic"     => array(
                    "rule"    => "isUnique",
                    "message" => "Username-ul este deja folosit"
                ),
            ),
            "password" => array(
                "parolaNotEmpty" => array(
                    "rule"    => "notEmpty",
                    "message" => "Parola nu poate fi gol"
                )
            )
        );

        public function beforeSave( $options = array() ) {
            if( isset( $this->data['User']['password'] ) ) {
                $this->data['User']['password'] = AuthComponent::password( $this->data['User']['password'] );
            }
            if( isset( $this->data['User']['subiecte_interes'] ) ) {
                $this->data['User']['subiecte_interes'] = str_replace( " ", ",", $this->data['User']['subiecte_interes'] );
            }
            return true;
        }
    }
